package com.luncheng.mcp.core.factory;

import com.luncheng.mcp.core.model.McpClientConfig;
import com.luncheng.mcp.core.strategy.TransportStrategy;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema;

import java.time.Duration;

public class McpClientFactory {
    public static McpSyncClient createClient(McpClientConfig mcpClientConfig) {
        TransportStrategy strategy = TransportFactory.getTransport(mcpClientConfig.getTransportType());
        McpClientTransport transport = strategy.createClientTransport(mcpClientConfig);
        McpSyncClient client = io.modelcontextprotocol.client.McpClient.sync(transport)
                .requestTimeout(Duration.ofSeconds(mcpClientConfig.getRequestTimeout()))
                .capabilities(McpSchema.ClientCapabilities.builder().roots(true).sampling().build())
                .sampling(request -> {
                    System.out.println("收到采样请求: " + request);
                    return McpSchema.CreateMessageResult.builder()
                            .role(McpSchema.Role.ASSISTANT)
                            .message("这是一个示例响应")
                            .build();
                })
                .build();
        client.initialize();
        return client;
    }
}